package com.lwm.proxy.client;

import com.lwm.proxy.agent.ProducerProxy;
import com.lwm.proxy.produce.IProducer;
import com.lwm.proxy.produce.Producer;

import java.lang.reflect.Proxy;

/**
 * 动态代理：
 * 特点：字节码随用随创建，随用随加载
 * 作用：不修改源码的基础上对已有功能进行增强
 * 分类：
 * 基于接口：Proxy JDK官方
 * 基于子类
 *
 * @author liweimin
 * @date 2021/6/10 17:28
 * @since
 */
public class Client {
    public static void main(String[] args) {
        Producer producer = new Producer();
        ProducerProxy producerProxy = new ProducerProxy(producer);

        IProducer instance = (IProducer) Proxy.newProxyInstance(Producer.class.getClassLoader(), new Class[]{IProducer.class}, producerProxy);
        instance.saleProduct(100);
        instance.afterService(100);

        Class<?> aClass = Proxy.getProxyClass(ProducerProxy.class.getClassLoader(), Producer.class.getInterfaces());
        System.out.println(aClass.getName());
        System.out.println(Proxy.isProxyClass(aClass));
        System.out.println("============================");
        System.out.println(instance.getClass());
        System.out.println("============================");

    }
}
